1   /*
2    * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.
8    *
9    * This code is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   * version 2 for more details (a copy is included in the LICENSE file that
13   * accompanied this code).
14   *
15   * You should have received a copy of the GNU General Public License version
16   * 2 along with this work; if not, write to the Free Software Foundation,
17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18   *
19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20   * or visit www.oracle.com if you need additional information or have any
21   * questions.
22   */
23  
24  /* @test
25     @bug 4262894 6233303
26     @summary Testing substitute character Escape sequence
27   */
28  
29  import java.nio.*;
30  import java.nio.charset.*;
31  
32  public class TestISO2022JPSubBytes {
33      /* \U2460 is not valid character in ISO2022JP and will be substituted
34       * with replacement character. If the replacement character is not the
35       * "current charset" character, correct escape sequence should be output
36       * for changing character set.
37       */
38      static char[][] in = { {'\u25cb', '\u2460', '\u25cb'},
39                             {'\u0061', '\u2460', '\u0061'},
40                             {'\u25cb', '\u2460', '\u25cb'},
41                             {'\u0061', '\u2460', '\u0061'},
42                           };
43      static byte[][] expected = { {0x1b, 0x24, 0x42, 0x21, 0x7b,
44                                    0x21, 0x29,
45                                    0x21, 0x7b,
46                                    0x1b, 0x28, 0x42},
47                                   {0x61,
48                                    0x1b, 0x24, 0x42, 0x21, 0x29,
49                                    0x1b, 0x28, 0x42, 0x61},
50                                   {0x1b, 0x24, 0x42, 0x21, 0x7b,
51                                    0x1b, 0x28, 0x42, 0x3f,
52                                    0x1b, 0x24, 0x42, 0x21, 0x7b,
53                                    0x1b, 0x28, 0x42},
54                                   {0x61,
55                                    0x3f,
56                                    0x61}
57                                  };
58  
59      public static void main(String args[]) throws Exception {
60          CharsetEncoder enc = Charset.forName("ISO2022JP")
61            .newEncoder()
62            .onUnmappableCharacter(CodingErrorAction.REPLACE);
63  
64          test(enc, in[0], expected[0]);
65  
66          enc.reset();
67          test(enc, in[1], expected[1]);
68  
69          enc.reset();
70          enc.replaceWith(new byte[]{(byte)'?'});
71          test(enc, in[2], expected[2]);
72  
73          enc.reset();
74          test(enc, in[3], expected[3]);
75      }
76  
77      public static void test (CharsetEncoder enc,
78                               char[] inputChars,
79                               byte[] expectedBytes) throws Exception
80      {
81          ByteBuffer bb = ByteBuffer.allocate(expectedBytes.length);
82          enc.encode(CharBuffer.wrap(inputChars), bb, true);
83          enc.flush(bb);
84          bb.flip();
85          byte[] outputBuff = bb.array();
86          int outputLen = bb.limit();
87          if (outputLen != expectedBytes.length) {
88              throw new Exception("Output bytes does not match");
89          }
90          for (int i = 0; i < outputLen; ++i) {
91              System.out.printf("<%x:%x> ",
92                                expectedBytes[i] & 0xff,
93                                outputBuff[i] & 0xff);
94              if (expectedBytes[i] != outputBuff[i]) {
95                  System.out.println("...");
96                  throw new Exception("Output bytes does not match");
97              }
98          }
99          System.out.println();
100     }
101 }